from rest_framework.viewsets import ModelViewSet,ViewSet,GenericViewSet
from rest_framework.permissions import IsAdminUser
from rest_framework.generics import ListAPIView,GenericAPIView
from django.db.models import Q
from rest_framework.response import Response

from goods.models import SKUImage, SKU, GoodsCategory, SPU, SPUSpecification
from meiduo_admin.serializers.skus import SKUImageSerializer, SKUSimpleSerializer,SKUSerializer, CategorySimpleSerializer, SPUSimpleSerializer,SPUSpecSerializer


# GET  /meiduo_admin/skus/images/?page=<页码>&page_size=<页容量>
class SKUImageViewSet(ModelViewSet):
    # 商品图片视图集
    # 设置权限
    permission_classes = [IsAdminUser]

    # 指定router动态生成路由时，提取参数的正则表达式
    lookup_value_regex = '\d+'

    serializer_class = SKUImageSerializer

    queryset = SKUImage.objects.all()


# GET /meiduo_admin/skus/simple/
class SKUSimpleView(ListAPIView):
    # 新增商品图片列表（简单SKU列表）

    permission_classes = [IsAdminUser]
    # 指定序列化器类
    serializer_class = SKUSimpleSerializer

    queryset = SKU.objects.all()

    # 关闭分页
    pagination_class = None


# GET /meiduo_admin/skus/?keyword=<名称|副标题>&page=<页码>&page_size=<页容量>
# class SKUViewSet(ViewSet):
class SKUViewSet(ModelViewSet):
    # 定义商品SKU视图集

    permission_classes = [IsAdminUser]
    # 指定router动态生成路由时，提取参数的正则表达式
    lookup_value_regex = '\d+'

    # 指定序列化器类
    serializer_class = SKUSerializer

    # 指定查询集 (有关键字 需重写查询集)
    def get_queryset(self):

        keyword = self.request.query_params.get('keyword')

        if not keyword:
            sku_qs = SKU.objects.all()
        else:
            sku_qs = SKU.objects.filter(Q(name__contain=keyword) |
                                          Q(caption_contain=keyword))
        return sku_qs

    # def list(self,request):
    #     keyword = request.query_params.get('keyword')
    #     if not keyword:
    #         sku_qs = SKU.objects.all()
    #     else:
    #         sku_qs = SKU.objects.filter(Q(name__contain=keyword) |
    #                                           Q(caption_contain=keyword))
    #     serializer = SKUSerializer(sku_qs,many=True)
    #     return Response(serializer.data)


# GET /meiduo_admin/skus/categories/
class SKUCategoriesView(ListAPIView):

    serializer_class = CategorySimpleSerializer

    queryset = GoodsCategory.objects.filter(subs=None)

    pagination_class = None


# GET /meiduo_admin/goods/simple/
class SPUSimpleView(ListAPIView):
    # spu商品简单展示视图

    permission_classes = [IsAdminUser]

    serializer_class = SPUSimpleSerializer

    queryset = SPU.objects.all()

    pagination_class = None


# GET meiduo_admin/goods/(?P<pk>\d+)/specs/
class SPUSpecView(ListAPIView):
# class SPUSpecView(GenericAPIView):
    # 获取SPU商品规格信息
    permission_class = [IsAdminUser]

    serializer_class = SPUSpecSerializer

    def get_queryset(self):
        """返回视图所使用的查询集"""
        # 获取pk
        pk = self.kwargs['pk']
        print(self.kwargs)
        return SPUSpecification.objects.filter(spu_id=pk)

    # 注：关闭分页
    pagination_class = None
    #
    # def get(self, request, pk):
    #     queryset = SPUSpecification.objects.filter(spu_id=pk)
    #     serializer = SPUSpecSerializer(queryset,many=True)
    #     return Response(serializer.data)




